package com.lanou.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.Article;
import com.lanou.entity.MyNotice;
import com.lanou.entity.User;
import com.lanou.mapper.ArticleMapper;
import com.lanou.mapper.MyNoticeMapper;
import com.lanou.mapper.UserMapper;
import com.lanou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author lizhenjiang
 * @Date 2025年08月13日 20:12
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyNoticeMapper myNoticeMapper;

    @Override
    public Article selectArticleById(Integer id) {
        return articleMapper.selectArticleById(id);
    }

    @Override
    public PageInfo<Article> selectArticleList(Article article, Integer pageNum, Integer pageSize) {
        if (pageNum == null && pageSize == null) {
            PageInfo<Article> pageInfo = new PageInfo<>(articleMapper.selectArticleList(article));
            return pageInfo;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.selectArticleList(article);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public Integer updateArticle(Integer id,String status,Integer userId) {
        Article article = articleMapper.selectArticleById(id);
        User user = userMapper.selectUser(userId);
        if(status.equals("点赞")){
            article.setLikeCount(article.getLikeCount() + 1);
            user.setPoints(user.getPoints() + 1);
            userMapper.updateUser(user);
            MyNotice myNotice = new MyNotice(null,"文章点赞","您为文章点赞，奖励您1积分","xlzs.png",new Date(),userId);
            myNoticeMapper.addMyNotice(myNotice);
        }
       if(status.equals("取消点赞")){
           article.setLikeCount(article.getLikeCount() - 1);
           user.setPoints(user.getPoints() - 1);
           userMapper.updateUser(user);
           MyNotice myNotice = new MyNotice(null,"取消点赞","您取消了文章点赞，减少1积分","xlzs.png",new Date(),userId);
           myNoticeMapper.addMyNotice(myNotice);
       }
       return articleMapper.updateArticle(article);
    }
}
